require(tidyverse)
require(patchwork)

rm(list = ls())
gc()

################################################################################
##
## Date:    2024-12-20
## Author:  james.h.bisbee@vanderbilt.edu
## Purpose: This script generates Figure 4.
## Inputs:  /scratch/jhb362/zilinsky_2023/data/results/VIMP_PDP/RF_2024_outcome.*-month.*lag1.RData
##            - Partial dependence estimates from random forest, estimated with NFR_pdp_prep.R
##            - Summarized on the NYU HPC into combined_res.RData via NFR_data_prep.R
## Outputs: ./figures/fig4.pdf
##
################################################################################

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()

load('./data/PDP_models/combined_res.RData')

pNATECONIMP <- raw %>%
  filter(outcome %in% c('NATECONIMP')) %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  scale_y_continuous(breaks = 1:3,labels = c('Getting\nworse','The\nsame','Getting\nbetter'),limits = c(1,3)) + 
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'Do you think that economic conditions in this\ncountry are getting better or getting worse?',
       color = 'Party',
       size = '# Responding',
       y = '')

pECON <- raw %>%
  filter(outcome %in% c('ECON')) %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  scale_y_continuous(breaks = 1:4,labels = c('Poor','Only\nfair','Good','Excellent'),limits = c(1,3)) + 
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'How would you rate economic conditions\nin this country today?',
       color = 'Party',
       size = '# Responding',
       y = '')

pLIFE5YR <- raw %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  filter(outcome %in% c('LIFE5YR')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  # scale_y_continuous(breaks = 1:3,labels = c('Getting worse','The same','Getting better'),limits = c(1,3)) + 
  scale_y_continuous(breaks = 1:10,limits = c(5,9)) + 
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'On which step of the ladder would you say\nyou will stand about five years from now?',
       color = 'Party',
       size = '# Responding',
       y = 'Life Ladder\n(0 = worst, 10 = best)')

pLIFETOD <- raw %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  filter(outcome %in% c('LIFETOD')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  # scale_y_continuous(breaks = 1:3,labels = c('Getting worse','The same','Getting better'),limits = c(1,3)) + 
  scale_y_continuous(breaks = 1:10,limits = c(5,9)) + 
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'On which step of the ladder would you say\nyou stand today?',
       color = 'Party',
       size = '# Responding',
       y = 'Life Ladder\n(0 = worst, 10 = best)')

pSOLSAT <- raw %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  filter(outcome %in% c('SOLSAT')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  # scale_y_continuous(breaks = 1:3,labels = c('Getting worse','The same','Getting better'),limits = c(1,3)) + 
  scale_y_continuous(labels = scales::percent) +
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'Are you satisfied or dissatisfied with your standard of living,\nall the things you can buy and do?',
       color = 'Party',
       size = '# Responding',
       y = '% Responding "Satisfied"')

pSTRESS <- raw %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  filter(outcome %in% c('STRESS')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  # scale_y_continuous(breaks = 1:3,labels = c('Getting worse','The same','Getting better'),limits = c(1,3)) + 
  scale_y_continuous(labels = scales::percent,limits = c(0,.35)) +
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'Did you experience stress\na lot of the day yesterday?',
       color = 'Party',
       size = '# Responding',
       y = '% Responding "Yes"')

pWORRY <- raw %>%
  filter(PARTY %in% c('Democrat','Republican')) %>%
  filter(outcome %in% c('WORRY')) %>%
  mutate(PARTY = factor(gsub('_',' ',gsub('_no_lean','',PARTY)),levels = c('Democrat','Lean Democrat','Independent','Lean Republican','Republican'))) %>%
  ggplot(aes(x = date,y = preds,color = PARTY)) + 
  geom_point() + 
  # geom_line() + 
  theme_bw() + 
  # facet_grid(outcome~.,scales = 'free') + 
  geom_vline(xintercept = as.Date(c('2009-01-17','2017-01-21'))) + 
  scale_color_manual(name = 'Party',values = c('Democrat' = 'darkblue','skyblue','grey80','tomato','Republican' = 'darkred')) + 
  # scale_y_continuous(breaks = 1:3,labels = c('Getting worse','The same','Getting better'),limits = c(1,3)) + 
  scale_y_continuous(labels = scales::percent,limits = c(0,.35)) +
  scale_x_date(limits = as.Date(c('2008-01-01','2019-01-01'))) + 
  labs(x = 'Interview Date',
       title = NULL,
       subtitle = 'Did you experience worry\na lot of the day yesterday?',
       color = 'Party',
       size = '# Responding',
       y = '% Responding "Yes"')


pdf('./figures/fig4.pdf',width = 8,height = 8)
((pSTRESS & labs(x = NULL)) + (pWORRY & labs(x = NULL,y = NULL))) /
  ((pLIFETOD & labs(x = NULL)) + (pLIFE5YR & labs(x = NULL,y = NULL))) / 
  ((pECON & labs(x = 'Interview Month')) + (pNATECONIMP & labs(x = 'Interview Month'))) + plot_layout(guides = 'collect') & theme(legend.position = 'bottom')
dev.off()

# EOF